import 'package:flustars/flustars.dart';
import 'package:drm/http/dioUtil.dart';
import 'package:bloc/bloc.dart';
import 'package:permission_handler/permission_handler.dart';


class Application {}

enum AppEvent { INI }

class AppBloc extends Bloc<AppEvent, bool> {
  @override
  bool get initialState {
    return false;
  }

  @override
  Stream<bool> mapEventToState(AppEvent event) async* {
    if (event == AppEvent.INI) {
      await iniSpUtils();
      iniAppUrl();
      await iniPermission();


      yield true;
    }
  }

  iniSpUtils() async {
    await SpUtil.getInstance();
  }

  iniAppUrl() {
    DioUtil.getInstance();
  }

  iniPermission() async {
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.storage);
    if (permission != PermissionStatus.granted) {
      Map<PermissionGroup, PermissionStatus> permissions =
          await PermissionHandler()
              .requestPermissions([PermissionGroup.storage]);

      StringBuffer sb = StringBuffer();

      permissions.forEach((permissionGroup,permissionStatus) {
             if ( permissionStatus != PermissionStatus.granted) {
               sb.write(permissionGroup.value);
             }
      });

      if (sb.toString().isNotEmpty) {
//        MyUtils.showToast('您拒绝部分权限，请手动设置');
        await PermissionHandler().openAppSettings();
      }
    }
  }
}
